<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" :rules="queryForm" label-width="68px">
      <el-form-item label="企业名称" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业类型" prop="compType">
        <el-select v-model="queryParams.compType" placeholder="请选择企业类型" clearable
                   size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物业服务类型" prop="ruleId" label-width="120px">
        <el-select v-model="queryParams.ruleId" placeholder="请选择"  clearable>
          <el-option
            v-for="item in ruleOptions"
            :key="item.payRule"
            :label="item.ruleName"
            :value="item.ruleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:company:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:company:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:company:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" prop="compId"/>
      <el-table-column label="企业名称" align="center" prop="compName"/>
      <el-table-column label="企业类型" align="center" prop="compType" :formatter="typeFormat"/>
      <el-table-column label="物业服务类型" align="center" prop="payRule.ruleName"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:company:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:company:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="540px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="企业名称" prop="compName">
          <el-input v-model="form.compName" placeholder="请输入企业名称" maxlength="30"/>
        </el-form-item>
        <el-form-item label="企业类型" prop="compType">
          <el-select v-model="form.compType" placeholder="请选择企业类型" style="width: 380px;">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物业服务类型" prop="ruleId">
          <el-select v-model="form.ruleId" placeholder="请选择" style="width: 380px;">
            <el-option
              v-for="item in ruleOptions"
              :key="item.payRule"
              :label="item.ruleName"
              :value="item.ruleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  getCompany,
  delCompany,
  addCompany,
  updateCompany,
  exportCompany
} from "@/api/server/companies/company";

export default {
  name: "Company",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //类型字典
      typeOptions: [],
      //缴费规则选项
      ruleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compName: null,
        compType: null,
        ruleId: null,
      },
      // 表单参数
      form: {}
      ,
      queryForm: {
        compName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },

      // 表单校验
      rules: {
        compName: [
          {required: true, message: '企业名称不能为空', trigger: 'blur'}
        ],
        compType: [
          {required: true, message: '企业类型不能为空', trigger: 'change'}
        ],
        ruleId: [
          {required: true, message: '物业服务类型不能为空', trigger: 'change'}
        ],
      }
    }
      ;
  },
  created() {
    this.getList();
    this.getDicts('t_company_type').then(response => {
      this.typeOptions = response.data
    })
    // this.getDicts('t_rule_name').then(response => {
    //   this.ruleOptions = response.data
    // })
  },
  mounted() {
    getCompany().then(response => {
      this.ruleOptions = response.payRules;
    })
  },
  methods: {
    /** 查询企业信息列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        compId: null,
        compName: null,
        compType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      ;
      this.resetForm("form");
    },
    //类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.compType)
    },
    //规则字典翻译
    // ruleFormat(row, column) {
    //   return this.selectDictLabel(this.ruleOptions, row.ruleName)
    // },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.compId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getCompany().then(response => {
        this.ruleOptions = response.payRules;
        this.open = true;
        this.title = "添加企业信息";
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const compId =
        row.compId || this.ids
      getCompany(compId).then(response => {
        this.form = response.data;
        this.ruleOptions = response.payRules
        this.open = true;
        this.title = "修改企业信息";
      })
      ;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.compId != null) {
              updateCompany(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              ;
            } else {
              addCompany(this.form).then(response => {
                if (response.data === 0) {
                  this.$confirm('该企业已存在', '提示', {
                    cancelButtonClass: 'btn-custom-cancel',
                    closeOnClickModal: false,
                    type: 'info'
                  })
                } else {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                }
              })
            }
          }
        }
      )
      ;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const compIds = row.compId || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCompany(compIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业信息数据项?', "提示", {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: "info"
      }).then(function () {
        return exportCompany(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
}
;
</script>
